package com.thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.LongAdder;

public class ThreadDemo {

	private LongAdder adder = new LongAdder();

	static class MyThread implements Runnable{
		private CountDownLatch cd;
		
		public MyThread(CountDownLatch cd) {
			this.cd = cd;
		}

		@Override
		public void run() {
			try {
				
				int i = 0;
				while (true) {
					System.out.println("睡眠一秒钟: "+ i++);
					Thread.sleep(5000);
					if (i == 2) {
						System.out.println("子线程打断主线程");
						cd.countDown();
					}
				}
				
			} catch (InterruptedException e) {
				System.out.println("现场被打断");
				e.printStackTrace();
			}
			
		}
		
	}

	public static void main(String[] args) {

	//	ExecutorService executorService = Executors.newFixedThreadPool(1);

		CountDownLatch cd = new CountDownLatch(1);
		Thread tt = new Thread(new MyThread(cd));
		tt.start();
		try {
			System.out.println("主线程等待子线程 countdown");
			Thread.sleep(8000);
			System.out.println("主线程打断子线程");
			tt.interrupt();
			
		} catch (InterruptedException e) {
			System.out.println("现场被打断22222");
			e.printStackTrace();
		}
		//
	}

}
